package com.pussinboots.morning.sync.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pussinboots.morning.sync.dao.OrderMapper;
import com.pussinboots.morning.sync.entity.Order;
import com.pussinboots.morning.sync.service.IOrderService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author BaoZhou
 * @since 2019-09-22
 */
@Service
@DS("master")
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    @Transactional
    public boolean changeBatchOrderStatusByOrderNumber(List<Order> orders) {
        try {
            orderMapper.updateBatchByOrderNumber(orders);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean changeOrderStatus(Long orderNumber, Integer status) {
        Order order = orderMapper.selectOne(new QueryWrapper<Order>().eq("order_number", orderNumber));
        if (order == null) {
            return false;
        }
        order.setOrderStatus(status);
        try {
            orderMapper.updateById(order);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<Long> getOrderNumbersByOrderNumbers(List<Long> orderNumbers) {
        //奇葩查询，在程序无结果，在navcat结果正常
        // 只需查一个列即可
        //selectList(new QueryWrapper<Order>().select("order_number","order_id").eq("order_number", orderNumbers))/*.in("order_number", orderNumbers))*/;
        List<Order> orderList = orderMapper.getOrderNumbers(orderNumbers);
        if (orderList.isEmpty()) {
            return null;
        }
        val orderNumbers_ = new ArrayList<Long>();
        for (Order order : orderList) {
            orderNumbers_.add(order.getOrderNumber());
        }
        return orderNumbers_;
    }
}